package com.zxp.server.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxp.server.config.Result;
import com.zxp.server.dto.UserLogin;
import com.zxp.server.mapper.UserMapper;
import com.zxp.server.service.IUserService;
import com.zxp.server.util.MD5Util;
import com.zxp.server.util.token.JwtUtil;
import com.zxp.server.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 朱小皮
 * @since 2022-05-04
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    UserMapper userMapper;

    /**
     *查询用户是否被注册
     */
    @Override
    public Boolean userExists(String username){

        QueryWrapper<User> wrapper = Wrappers.query();
        wrapper.eq("username",username);
        User user = userMapper.selectOne(wrapper);
        return user == null;
    }

    /**
     *注册用户
     */
    @Override
    public int addUser(User user){
        //使用MD5加密算法加密
        user.setPassword(MD5Util.getMD5(user.getPassword()));
        return userMapper.insert(user);
    }


    @Override
    public User findUserById(String userId) {
        return userMapper.selectById(userId);
    }

    /**
     *
     * @param user
     */
    @Override
    public void updateUser(User user){
        userMapper.updateById(user);
    }

    /**
     *
     * @param userLogin
     * @param Request
     * @return
     */
    @Override
    public Result login(UserLogin userLogin, HttpServletRequest Request){
        String username = userLogin.getUsername();
        String password = userLogin.getPassword();
        String code = userLogin.getCaptcha();
        String Kaptcha = (String) Request.getSession().getAttribute("captcha");
        if (StringUtils.isEmpty(code) ||StringUtils.isEmpty(Kaptcha)|| !Kaptcha.equalsIgnoreCase(code)) {
            return Result.error("验证码不正确，请重新输入！");
        }
        HashMap<String, Object> userInfo = new HashMap<>();
        userInfo.put("username",username);
        userInfo.put("password",MD5Util.getMD5(password));
        //通过用户密码查询用户信息
        List<User> users = userMapper.selectByMap(userInfo);
        //用户信息不存在
        if (users.size()==0){
            return Result.error("用户名或密码不正确，请重新输入！");
        }
        User user = users.get(0);
        //判断用户是否被禁用
        if (user.getUserstate()==0){
            return Result.error("用户被禁用，请联系管理员");
        }

        //用户信息检查完成，开始发放token
        String jwt = JwtUtil.createJWT(user);
        /*Map<String, String> tokenMap = new HashMap<>();
        tokenMap.put("token", jwt);
        tokenMap.put("tokenHead", "tokenHead");//返回头部信息*/
        return Result.success("登陆成功",jwt);
    }
}
